//
//  M3d.swift
//  WriteToLocal
//
//  Created by 郑东喜 on 2017/3/4.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

class M3d: UITabBarController {

    var tabbarIcon : NSDictionary? {
        didSet {
            
            print("\((#file as NSString).lastPathComponent):(\(#line))\n")
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    
        
        let vc1 = UIViewController()
        vc1.view.backgroundColor = UIColor.randomColor()
        
        let vc2 = UIViewController()
        vc2.view.backgroundColor = UIColor.randomColor()
        
        let vc3 = UIViewController()
        vc3.view.backgroundColor = UIColor.randomColor()
        
        let vc4 = UIViewController()
        vc4.view.backgroundColor = UIColor.randomColor()
        
        let vc5 = UIViewController()
        vc4.view.backgroundColor = UIColor.randomColor()
        
        let nav = UINavigationController.init(rootViewController: vc1)
        
        let nav2 = UINavigationController.init(rootViewController: vc2)
        
        let nav3 = UINavigationController.init(rootViewController: vc3)
        
        let nav4 = UINavigationController.init(rootViewController: vc4)
        
        let nav5 = UINavigationController.init(rootViewController: vc5)
        
        
        

        
        self.setupChildVC(nav, title: "vc1", imageName: "homeA", selectImageName: "homeL")
        self.setupChildVC(nav2, title: "vc2", imageName: "reportA", selectImageName: "reportL")
        
        self.setupChildVC(nav3, title: "vc3", imageName: "messageA", selectImageName: "messageL")
        
        
        self.setupChildVC(nav4, title: "vc4", imageName: "messageA", selectImageName: "messageL")
        
        self.setupChildVC(nav5, title: "vc5", imageName: "messageA", selectImageName: "messageL")
        
//        let str = "BvptgR3d/oE="
        
//        print("\((#file as NSString).lastPathComponent):(\(#line))\n",str.threeDESEncryptOrDecrypt(op: CCOperation(kCCDecrypt), key: "易米科技有限公司", iv: ""))
    }
    
    //添加子页面
    func setupChildVC(_ childVC: UIViewController,title: String,imageName: String,selectImageName: String) {
        
        childVC.title = title
        childVC.tabBarItem.image = UIImage.init(named: imageName)
        //        不在渲染图片
        childVC.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.withRenderingMode(.alwaysOriginal)
        
        self.addChildViewController(childVC)
    }

}


extension String {
    
    /**
     3DES的加密过程 和 解密过程
     
     - parameter op : CCOperation： 加密还是解密
     CCOperation（kCCEncrypt）加密
     CCOperation（kCCDecrypt) 解密
     
     - parameter key: 专有的key,一个钥匙一般
     - parameter iv : 可选的初始化向量，可以为nil
     - returns      : 返回加密或解密的参数
     */
    func threeDESEncryptOrDecrypt(op: CCOperation,key: String,iv: String) -> String? {
        
        // Key
        let keyData: NSData = (key as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        let keyBytes         = UnsafeMutableRawPointer(mutating: keyData.bytes)
        
        // 加密或解密的内容
        var data: NSData = NSData()
        if op == CCOperation(kCCEncrypt) {
            data  = (self as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        }
        else {
            data =  NSData(base64Encoded: self, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)!
        }
        
        let dataLength    = size_t(data.length)
        let dataBytes     = UnsafeMutableRawPointer(mutating: data.bytes)
        
        // 返回数据
        let cryptData    = NSMutableData(length: Int(dataLength) + kCCBlockSize3DES)
        let cryptPointer = UnsafeMutableRawPointer(cryptData!.mutableBytes)
        let cryptLength  = size_t(cryptData!.length)
        
        //  可选 的初始化向量
        let viData :NSData = (iv as NSString).data(using: String.Encoding.utf8.rawValue) as NSData!
        let viDataBytes    = UnsafeMutableRawPointer(mutating: viData.bytes)
        
        // 特定的几个参数
        let keyLength              = size_t(kCCKeySize3DES)
        let operation: CCOperation = UInt32(op)
        let algoritm:  CCAlgorithm = UInt32(kCCAlgorithm3DES)
        let options:   CCOptions   = UInt32(kCCOptionPKCS7Padding)
        
        var numBytesCrypted :size_t = 0
        
        let cryptStatus = CCCrypt(operation, // 加密还是解密
            algoritm, // 算法类型
            options,  // 密码块的设置选项
            keyBytes, // 秘钥的字节
            keyLength, // 秘钥的长度
            viDataBytes, // 可选初始化向量的字节
            dataBytes, // 加解密内容的字节
            dataLength, // 加解密内容的长度
            cryptPointer, // output data buffer
            cryptLength,  // output data length available
            &numBytesCrypted) // real output data length
        
        
        
        if UInt32(cryptStatus) == UInt32(kCCSuccess) {
            
            cryptData!.length = Int(numBytesCrypted)
            if op == CCOperation(kCCEncrypt)  {
                let base64cryptString = cryptData?.base64EncodedString(options: .lineLength64Characters)
                return base64cryptString
            }
            else {
                //   let base64cryptString = NSString(bytes: cryptPointer, length: cryptLength, encoding: NSUTF8StringEncoding) as? String  // 用这个会导致返回的JSON数据格式可能有问题，最好不用
                let base64cryptString = NSString(data: cryptData! as Data,  encoding: String.Encoding.utf8.rawValue) as? String
                return base64cryptString
            }
        } else {
            print("Error: \(cryptStatus)")
        }
        return nil
    }
    
    
}
